<template>
	<view class="content">
		<!-- 占位 -->
		<view class="" style="height: 20rpx;"></view>

		<!-- 店铺 => 仓库 -->
		<view class="item" v-if="standing == 3 && type == 2">
			<view class="item-title">{{$t("express.store_name.out.aim1")}}</view>
			<uni-data-select v-model="stashItem" :localdata="stashRange"
				:placeholder="$t('login.please_standing')"></uni-data-select>
		</view>

		<!-- 店铺 => 店铺 -->
		<view class="item" v-if="standing == 3 && type == 3">
			<view class="item-title">{{$t("express.store_name.out.aim2")}}</view>
			<uni-data-select v-model="shopItem" :localdata="shopRange"
				:placeholder="$t('login.please_standing')"></uni-data-select>
		</view>

		<expressSheet :orderData="orderData" :type="'out'" :aimType="type"></expressSheet>

		<!-- 占位 -->
		<view class="" style="height: 20rpx;"></view>
		<view class="" style="height: 88rpx;"></view>
		<view class="footer" @click="handleSweep">{{submit}}{{ standing }}=={{ type }}</view>
	</view>
</template>

<script>
	import expressSheet from './components/expressSheet.vue'

	export default {
		components: {
			expressSheet
		},
		data() {
			return {
				type: '0',
				title: '',
				orderData: {
					"sku_images": [],
					"color": "",
					"size": "",
					"material_quality": "",
					"store_name": "",
					"goods_title": "",
					"skc_number": "",
					"goods_code": "",
					"qrcode_url": "",
					"barcode": "",
					"ctime": "",
					"goods_number": 0,
					"sku_nuber": ""
				},
				submit: this.$t("sweep") + ' - ' + this.$t("index.out"),
				optionType: true,
				submitType: true,
				goods_code: "",
				stashItem: "", // 仓库item
				stashRange: [], // 仓库列表 
				shopItem: "", // 店铺item
				shopRange: [], // 店铺列表
				standing: 0, // 登录身份
			}
		},
		onLoad(option) {
			if (option.text) {
				uni.setNavigationBarTitle({
					title: option.text
				})
			}
			if (option.value) this.type = option.value
			// 店铺 => 仓库
			if (this.standing == 3 && option.value == 2) {
				this.getStoreList()
			}
			// 店铺 => 店铺
			if (this.standing == 3 && option.value == 3) {
				this.getShopList()
			}
		},
		onShow() {
			let that = this
			that.standing = uni.getStorageSync("standing")
			uni.$on('isRefresh', async function(data) {
				// TODO 下面执行刷新的方法
				// data =
				// 	"https://africaadmin.lastinglight.cn/cxboiZCapj.php/deliverys/fahuo/returnJson?goods_code=PC20240810112074"
				const [err, res] = await uni.request({
					url: data,
				});
				// uni.showModal({
				// 	title: "err/res",
				// 	content: 'url:' + data + "res.data.data:" + JSON.stringify(res.data.data)
				// })
				that.goods_code = data.split("=")[1]
				if (res.data.code == 200) {
					that.orderData = res.data.data[0]
					that.submit = this.$t("verify") + ' - ' + this.$t("index.out")
					that.optionType = false
				}
			})
		},
		methods: {
			handleSweep() {
				if (this.optionType) {
					uni.navigateTo({
						url: "/pages/qrcode/index",
					})
				} else {
					let that = this
					let userInfo = uni.getStorageSync("userInfo")
					let standing = uni.getStorageSync("standing")
					if (!that.submitType) return
					that.submitType = false

					// if (standing == 3) {
					// 	console.log("店铺出库接口");
					// 	if (that.type == 2) {
							// console.log("店铺货物调拨接口");
							// if (!that.stashItem) {
							// 	that.submitType = true
							// 	return uni.showToast({
							// 		title: "Please select aim",
							// 		icon: "none"
							// 	})
							// }
							that.httpPost("/api/RemovePackage/storeSaleRemove",`shop_id=${userInfo.id}&goods_code=${that.goods_code}&store_id=${userInfo.id}`)
						// } else if (that.type == 3) {
						// 	that.httpPost("/api/RemovePackage/shopShopRemove",`shop_id=${userInfo.id}&goods_code=${that.goods_code}&aim_shop_id=${that.shopItem}`)
						// }
					// }
				}
			},
			// 获取店铺列表
			getShopList() {
				let userInfo = uni.getStorageSync('userInfo')
				let url = this.$http + "/RemovePackage/getShopList"
				this.$ajax(url, "GET", null, (response) => {
					let res = JSON.parse(response)
					if (res.code == 200) {
						this.shopRange = res.data.map(item => {
							return {
								value: item.id,
								text: item.shop_name
							}
						})
						this.shopRange = this.shopRange.filter(item => item.value != userInfo.id)
					}
				})
			},

			// 获取仓库接口
			getStoreList() {
				let url = this.$http + "/RemovePackage/getStoreList"
				this.$ajax(url, "GET", null, (response) => {
					let res = JSON.parse(response)
					if (res.code == 200) {
						this.stashRange = res.data.map(item => {
							return {
								value: item.id,
								text: item.store_name
							}
						})
					}
				})
			},

			// post请求函数
			httpPost(url = "", data = {}) {
				url = url.replace("/api", this.$http)
				this.$ajax(url, "POST", data, (response) => {
					let res = JSON.parse(response)
					// uni.showModal({
					// 	title: "res",
					// 	content: 'url:' + url + "data:" + data + "response:" + response
					// })
					if (res.code == 200) {
						this._toastBack(res.msg)
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
							success: () => {
								setTimeout(() => {
									uni.navigateBack()
									this.submitType = !this.submitType
								}, 1200)
							}
						})
					}
				})
			},

			// 提示语+返回上一页
			_toastBack(msg) {

				uni.showToast({
					title: msg,
					icon: "none",
					success: () => {
						this.submitType = true
						setTimeout(() => {
							uni.navigateBack()
						}, 1200)
					}
				})
			},

			// 预览图片
			previewImage(index) {
				uni.previewImage({
					urls: this.orderData.sku_images,
					current: index
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background: #fff;
	}

	.content-box {
		width: 100vw;
		background: #fff;
		padding: 0 24rpx;
	}

	.content-box .item:last-child {
		border: none;
	}

	.item {
		height: 124rpx;
		display: flex;
		align-items: center;
		border-bottom: 1rpx solid #DFE1E6;
		font-size: 32rpx;
		padding: 0 34rpx;
	}

	.item-title {
		color: #15161A;
		width: 200rpx;
		font-weight: 700;
	}

	.item-img {
		width: 80rpx;
		height: 80rpx;
		margin-right: 8rpx;
	}

	.item-tag {
		color: #C9CACC;
	}

	.footer {
		width: 100%;
		line-height: 88rpx;
		background: #FF3C3F;
		color: #fff;
		position: fixed;
		bottom: 0;
		text-align: center;
	}
</style>